package com.hbm.eduservice.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbm.commonutils.R;
import com.hbm.eduservice.client.UcentermemberClient;
import com.hbm.eduservice.entity.EduComment;
import com.hbm.eduservice.entity.EduCourse;
import com.hbm.eduservice.entity.frontvo.CommentVo;
import com.hbm.eduservice.service.EduCommentService;
import com.hbm.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/eduservice/eduservice")
@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private UcentermemberClient ucentermemberClient;

    @GetMapping("pageComment/{current}/{limit}/{courseId}")
    public R pageComment(@PathVariable long current,@PathVariable long limit,@PathVariable String courseId ){
        Page<EduComment> page = new Page<>(current,limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        wrapper.eq("course_id",courseId);
        commentService.page(page,wrapper);

        long total = page.getTotal();
        List<EduComment> records = page.getRecords();
//        long pages = page.getPages();
//        boolean next = page.hasNext();
//        boolean previous = page.hasPrevious();

        return R.ok().data("total",total).data("records",records);
    }

    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment eduComment, HttpServletRequest request){
        //EduComment eduComment = new EduComment();
//        String courseId = CommentVo.getCourseId();
//
//        //根据课程id查询讲师id
//        EduCourse eduCourse = courseService.getById(courseId);
//        String teacherId = eduCourse.getTeacherId();
//
//        //获取用户id
//        String userId = CommentVo.getUserId();


        //BeanUtils.copyProperties(CommentVo,eduComment );
        R memberInfo = ucentermemberClient.getMemberInfo(request);
        System.out.println("++++++++++++++=");
        System.out.println( memberInfo);
        System.out.println("++++++++++++++=");
        commentService.save(eduComment);
        return R.ok();
    }

}

